import requests
import re
import random
import json


def url_response(url='https://httpbin.org/ip', proxy=None):
    '''
    这是一个利用requests.get()获取response响应的函数，支持添加代理，默认没有开启代理
    :param url:url地址
    :param proxy:代理地址 格式示例：101.200.36.219:80
    :return:response响应
    '''
    header = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36'
    }
    # 注意：这里建议 http https 两个都添加
    proxies = {
        'http': '{}'.format(proxy),  # 设置代理：代理格式示例：223.245.38.117:65309
        'https': '{}'.format(proxy)
    }
    try:
        if proxy:
            response = requests.get(url, headers=header, proxies=proxies, timeout=10)
        else:
            response = requests.get(url, headers=header, timeout=5)
    except:
        print('访问失败，请查看您的代理或网络环境...')
        return None

    return response  # 该函数放回response响应


def test_proxy(response):  # 判断IP和代理是否一致
    if proxy.split(':')[0] == json.loads(response.text)['origin']:
        # if result == proxy.split(':')[0]:
        print('当前IP和设置的代理 [{}] 代理已生效...'.format(proxy))
        return True
    else:
        print('代理未生效/不可用...')
        return False


proxies = [
    "61.183.176.122:57210",

]

if __name__ == '__main__':
    # 以下三个url可以直接测试当前IP。任选一个即可
    url = 'https://httpbin.org/ip'
    # url = 'http://httpbin.org/ip'
    # url = 'http://icanhazip.com/'

    proxy = '118.89.234.236:8787'
    r = url_response(url)
    print(r.content.decode())
    print(r.status_code)
    test_proxy(r)  # 调用判断函数
